<?php
//No direct access
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.view');

class OwnserviceViewBranches extends JView
{
	function display($tpl=null) {

		global $mainframe, $option;

		//Initialise variables
		$user		= & JFactory::getUser();
		$ownuser	= & Ownservice::getUser();
		$document	= & JFactory::getDocument();
		$db			= & JFactory::getDBO();
		$params 	= & $mainframe->getParams('com_ownservice');

		if($user->gid < 25) {
			if (!Ownservice::checkAccess( 'branch', 'manage' )) {
				JError::raiseWarning(500, JText::_('ALERTNOTAUTH') );
				$mainframe->redirect(JRoute::_('index.php?option=com_ownservice'));
			}
		}

		$filter_order		= $mainframe->getUserStateFromRequest($option.'.branch.filter_order', 'filter_order', 'name', 'cmd');
		$filter_order_Dir	= $mainframe->getUserStateFromRequest($option.'.branch.filter_order_Dir', 'filter_order_Dir', '', 'cmd');
		$search 			= $mainframe->getUserStateFromRequest($option.'.branch.fields.search', 'search', '', 'string' );
		$search 			= $db->getEscaped( trim(JString::strtolower( $search ) ) );

		//add css to document
		$document->addStyleSheet(JURI::base().'components/com_ownservice/assets/css/style.css');
		$document->addStyleSheet(JURI::base().'components/com_ownservice/assets/css/nav.css');
		$document->addStyleSheet(JURI::base().'components/com_ownservice/assets/css/icon.css');
		$document->addScript(JURI::base().'components/com_ownservice/assets/js/ownservice.js');

		//Get data form the model
		$model		= $this->getModel();
		$rows		= & $this->get('Data');
		$level		= $ownuser->get('level_id');
		$pageNav	= & $this->get('Pagination');
		$profile	= $model->getUser($user->get('id'));

		// Create a user access object for the current user
		$access = new stdClass();
		$access->canView		= ($user->gid < 25)? Ownservice::checkAccess('branch','view','branch','all'):1;
		$access->canEdit		= ($user->gid < 25)? Ownservice::checkAccess('branch','edit','branch','all'):1;
		$access->canAdd			= ($user->gid < 25)? Ownservice::checkAccess('branch','add','branch','all'):1;
		$access->canDelete		= ($user->gid < 25)? Ownservice::checkAccess('branch','delete','branch','all'):1;

		$lists = array();

		$lists['order']     = $filter_order;
		$lists['order_Dir'] = $filter_order_Dir;

		//set page title
		$params->set('page_title',	JText::_('OWNSERVICE_BRANCHES'));
		$document->setTitle( $params->get('page_title') );

		// search filter
		$lists['search']= $search;

		$this->assignRef('rows', $rows);
		$this->assignRef('pageNav', $pageNav);
		$this->assignRef('access', $access);
		$this->assignRef('lists', $lists);
		$this->assignRef('user', $user);
		$this->assignRef('ownuser', $ownuser);
		$this->assignRef('profile', $profile);

		parent::display($tpl);
	}
}